<!DOCTYPE html>
<html lang="" xml:lang="">
  <head>
    <title>Bayesian inference</title>
    <meta charset="utf-8" />
    <meta name="author" content="datasciencebox.org" />
    <script src="libs/header-attrs/header-attrs.js"></script>
    <link href="libs/font-awesome/css/all.css" rel="stylesheet" />
    <link href="libs/font-awesome/css/v4-shims.css" rel="stylesheet" />
    <link href="libs/panelset/panelset.css" rel="stylesheet" />
    <script src="libs/panelset/panelset.js"></script>
    <link rel="stylesheet" href="../xaringan-themer.css" type="text/css" />
    <link rel="stylesheet" href="../slides.css" type="text/css" />
  </head>
  <body>
    <textarea id="source">
class: center, middle, inverse, title-slide

.title[
# Bayesian inference
]
.subtitle[
## <br><br> Data Science in a Box
]
.author[
### <a href="https://datasciencebox.org/">datasciencebox.org</a>
]

---





layout: true
  
&lt;div class="my-footer"&gt;
&lt;span&gt;
&lt;a href="https://datasciencebox.org" target="_blank"&gt;datasciencebox.org&lt;/a&gt;
&lt;/span&gt;
&lt;/div&gt; 

---



class: middle

# Bayes' Theorem

---

## Free spam

.question[
You have 100 emails in your inbox: 60 are spam, 40 are not. Of the 60 spam emails, 35 contain the word "free". Of the rest, 3 contain the word "free". If an email contains the word "free", what is the probability that it is spam?
]

&lt;img src="img/spam-tree.png" width="50%" style="display: block; margin: auto;" /&gt;

`$$P(spam~|~free) = \frac{\#~spam~\&amp;~free}{\#~free} = \frac{35}{35+3} = 0.92$$`

---

## Bayes' Theorem

`$$P(A~|~B) = \frac{P(A~and~B)}{P(B)}$$`

--

- Then, `\(P(A~and~B) = P(A~|~B) \times P(B)\)`

--

- If A and B are independent, then knowing B doesn't tell us anything about A, i.e. `\(P(A~|~B) = P(A)\)`. Then,

`$$P(A~and~B) = P(A~|~B) \times P(B)$$`

---

class: middle

# Let's play

---

## Dice game

.question[
&lt;b&gt;What's the chance of winning?&lt;/b&gt; What is the probability of getting an outcome greater than or equal to 4 when rolling a 6-sided die? What is the probability when rolling a 12-sided die?
]

&lt;br&gt;

--

.question[
&lt;b&gt;Pick the "good" die.&lt;/b&gt; You're playing a game where you win if the die roll is greater than or equal to 4. If you could get your pick, which die would you prefer to play this game with, 6 or 12-sided?
]

---

class: small

## Set up

- I have two dice: one 6-sided, the other 12-sided. 

--

- We're going to play a game: I keep one die on the left side (die L) and one die on the right (die R), and you won't know which is the 6-sided die and which is the 12-sided. *When I say left, I mean YOUR left.*

--

- You pick die (L or R), I roll it, and I tell you if you win or not, where winning is getting a number greater than or equal to 4. If you win, you get candy on Wednesday. If you lose, no candy for you.

--

- We'll play this multiple times with different contestants.

--

- I will not swap the sides the dice are on at any point.

--

- We'll record which die each contestant picks and whether they won or lost.

--

- The ultimate goal is to come to a class consensus about whether the die on the left or the die on the right is the "good die".

--

- You get to pick how long you want play, but remember, each time you get &lt;4, you lose a piece of candy (so there is a cost associated with too many tries). If you make the wrong decision, you lose all the candy.

---

## Hypotheses and decisions

| Decision 	|   Truth: R good, L bad  |   Truth: R bad, L good  |
|----------	|:-----------------------:|:-----------------------:|
| Pick R   	| You get the candy!      |  You lose the candy :( 	|
| Pick L   	| You lose the candy :(   |  You get the candy!    	|

&lt;br&gt;

--

- At each trial you risk losing pieces of candy if you lose (the die comes up `\(&lt;\)` 4). Too many trials means you won't have much candy left. 

- And if you take too long you'll be stuck here for a while.

---

class: middle

# Setting a prior

---

## Initial guess

.question[
You have no idea if I have chosen the die on the left (L) to be the good die (12-sided) or bad die (6-sided). Then, before we collect any data, what are the probabilities associated with the following hypotheses?
]

`\(H_1\)`: R good, L bad
                                                        
`\(H_2\)`: R bad, L good

---

## Prior propbabilities

- These are your **prior probabilities** for the two competing claims (hypotheses): 
    * `\(H_1\)`: R good, L bad
    * `\(H_2\)`: R bad, L good

- That is, these probabilities represent what you believe before seeing any data.

- You could have conceivably made up these probabilities, but instead you have chosen to make an educated guess.

---

class: middle

# Observe and decide

---

## Data collection and making a decision

|        | Choice (L or R) | Result (W or L) |
|--------|-----------------|-----------------|
| Roll 1 |                 |                 |
| Roll 2 |                 |                 |
| Roll 3 |                 |                 |
| ...    |       ...       |       ...       |

&lt;br&gt;

.question[
What is your decision? How did you make this decision?
]

---

class: middle

# Posterior probability

---

## Roll 1

.question[
What is the probability, based on the outcome of the first roll, that R is the good die (and L is the bad die)?
]

---

## Posterior probability

- The probability we just calculated P(R is good | Win) is also called the **posterior probability**.

- Posterior probability is generally defined as P(hypothesis | data). It tells us the probability of a hypothesis we set forth, given the data we just observed. It depends on both the prior probability we set and the observed data.

- This is different than p-values -- the probability of observed or more extreme data given the null hypothesis being true, i.e. P(data | hypothesis).

---

## Updating the prior

- In the Bayesian approach, we evaluate claims iteratively as we collect more data.

- In the next iteration (roll) we get to take advantage of what we learned from the data.

- In other words, we **update** our prior with our posterior probability from the previous iteration.

---

## Roll 2

.question[
What is the probability, based on the outcome of the first two rolls, that R is the good die (and L is the bad die)?
]

---

class: middle

# Recap

---

## Recap: Bayesian inference

- Take advantage of prior information, like a previously published study or a physical model.

--

- Naturally integrate data as you collect it, and update your priors.

--

- Avoid the counter-intuitive Frequentist definition of a p-value as the P(observed or more extreme outcome | `\(H_0\)` is true). Instead base decisions on the posterior probability, P(hypothesis is true | observed data).

--

- **Watch out!** A good prior helps, a bad prior hurts, but the prior matters less the more data you have.

--

- More advanced Bayesian techniques offer flexibility not present in Frequentist models.

---

class: middle

# Breast cancer screening

---

class: middle, center

&lt;iframe width="900" height="500" src="https://www.cbsnews.com/embed/videos/breaking-down-the-new-mammogram-guidelines/" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;

---

## Breast cancer screening

- American Cancer Society estimates that about 1.7% of women have breast cancer. 

http://www.cancer.org/cancer/cancerbasics/cancer-prevalence

- Susan G. Komen For The Cure Foundation states that mammography correctly identifies about 78% of women who truly have breast cancer.

http://ww5.komen.org/BreastCancer/AccuracyofMammograms.html

- An article published in 2003 suggests that up to 10% of all mammograms are false positive. 

http://www.ncbi.nlm.nih.gov/pmc/articles/PMC1360940

**Note:** These percentages are approximate, and very difficult to estimate.

---

## Determining the prior

.question[
Prior to any testing and any information exchange between the patient and the doctor, what probability should a doctor assign to a female patient having breast cancer?
]

---

## Calculating the posterior

.question[
When a patient goes through breast cancer screening there are two competing claims: patient had cancer and patient doesn't have cancer. If a mammogram yields a positive result, what is the probability that patient has cancer, i.e. what is the posterior probability of having cancer if mammogram yield a positive result?
]

---

## Updating the prior when retesting

.question[
Suppose this patient who got a positive result in the first test wants to get tested again. What should the new prior probability that this patient has cancer? Is this probability smaller, larger, or equal to the prior probability in the first test? Why?
]

---

## Thinking ahead

.question[
If this patient tests positive in the second test as well, will the posterior probability of her having cancer be higher or lower (or equal to) the earlier posterior probability we calculated?
]

---

## Re-calculating the posterior when retesting

.question[
What is the posterior probability of having cancer if this second mammogram also yielded a positive result?
]

---

## Role of computation

.question[
We have done a bunch of hand calculations so far. How can we use computation in this paradigm?
]
    </textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"ratio": "16:9",
"highlightLines": true,
"highlightStyle": "solarized-light",
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
  window.dispatchEvent(new Event('resize'));
});
(function(d) {
  var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
  if (!r) return;
  s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
  d.head.appendChild(s);
})(document);

(function(d) {
  var el = d.getElementsByClassName("remark-slides-area");
  if (!el) return;
  var slide, slides = slideshow.getSlides(), els = el[0].children;
  for (var i = 1; i < slides.length; i++) {
    slide = slides[i];
    if (slide.properties.continued === "true" || slide.properties.count === "false") {
      els[i - 1].className += ' has-continuation';
    }
  }
  var s = d.createElement("style");
  s.type = "text/css"; s.innerHTML = "@media print { .has-continuation { display: none; } }";
  d.head.appendChild(s);
})(document);
// delete the temporary CSS (for displaying all slides initially) when the user
// starts to view slides
(function() {
  var deleted = false;
  slideshow.on('beforeShowSlide', function(slide) {
    if (deleted) return;
    var sheets = document.styleSheets, node;
    for (var i = 0; i < sheets.length; i++) {
      node = sheets[i].ownerNode;
      if (node.dataset["target"] !== "print-only") continue;
      node.parentNode.removeChild(node);
    }
    deleted = true;
  });
})();
// add `data-at-shortcutkeys` attribute to <body> to resolve conflicts with JAWS
// screen reader (see PR #262)
(function(d) {
  let res = {};
  d.querySelectorAll('.remark-help-content table tr').forEach(tr => {
    const t = tr.querySelector('td:nth-child(2)').innerText;
    tr.querySelectorAll('td:first-child .key').forEach(key => {
      const k = key.innerText;
      if (/^[a-z]$/.test(k)) res[k] = t;  // must be a single letter (key)
    });
  });
  d.body.setAttribute('data-at-shortcutkeys', JSON.stringify(res));
})(document);
(function() {
  "use strict"
  // Replace <script> tags in slides area to make them executable
  var scripts = document.querySelectorAll(
    '.remark-slides-area .remark-slide-container script'
  );
  if (!scripts.length) return;
  for (var i = 0; i < scripts.length; i++) {
    var s = document.createElement('script');
    var code = document.createTextNode(scripts[i].textContent);
    s.appendChild(code);
    var scriptAttrs = scripts[i].attributes;
    for (var j = 0; j < scriptAttrs.length; j++) {
      s.setAttribute(scriptAttrs[j].name, scriptAttrs[j].value);
    }
    scripts[i].parentElement.replaceChild(s, scripts[i]);
  }
})();
(function() {
  var links = document.getElementsByTagName('a');
  for (var i = 0; i < links.length; i++) {
    if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
      links[i].target = '_blank';
    }
  }
})();
// adds .remark-code-has-line-highlighted class to <pre> parent elements
// of code chunks containing highlighted lines with class .remark-code-line-highlighted
(function(d) {
  const hlines = d.querySelectorAll('.remark-code-line-highlighted');
  const preParents = [];
  const findPreParent = function(line, p = 0) {
    if (p > 1) return null; // traverse up no further than grandparent
    const el = line.parentElement;
    return el.tagName === "PRE" ? el : findPreParent(el, ++p);
  };

  for (let line of hlines) {
    let pre = findPreParent(line);
    if (pre && !preParents.includes(pre)) preParents.push(pre);
  }
  preParents.forEach(p => p.classList.add("remark-code-has-line-highlighted"));
})(document);</script>

<script>
slideshow._releaseMath = function(el) {
  var i, text, code, codes = el.getElementsByTagName('code');
  for (i = 0; i < codes.length;) {
    code = codes[i];
    if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
      text = code.textContent;
      if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
          /^\$\$(.|\s)+\$\$$/.test(text) ||
          /^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
        code.outerHTML = code.innerHTML;  // remove <code></code>
        continue;
      }
    }
    i++;
  }
};
slideshow._releaseMath(document);
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src  = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';
  if (location.protocol !== 'file:' && /^https?:/.test(script.src))
    script.src  = script.src.replace(/^https?:/, '');
  document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
  </body>
</html>
